package com.cskaoyan.service.impl;

import com.cskaoyan.bean.data.CollectListData;
import com.cskaoyan.bean.data.PageData;
import com.cskaoyan.bean.vo.CollectAddOrDeleteVo;
import com.cskaoyan.mapper.MarketCollectMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.model.MarketAddress;
import com.cskaoyan.model.MarketCollect;
import com.cskaoyan.model.MarketCollectExample;
import com.cskaoyan.model.MarketGoods;
import com.cskaoyan.service.CollectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CollectServiceImpl implements CollectService {
    @Autowired
    MarketCollectMapper collectMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;

    @Override
    public Integer hasCollect(int userId, Integer valueId, byte type) {
        MarketCollectExample collectExample = new MarketCollectExample();
        collectExample.createCriteria().andTypeEqualTo(type)
                .andUserIdEqualTo(userId)
                .andValueIdEqualTo(valueId)
                .andDeletedEqualTo(false);

        List<MarketCollect> collectList = collectMapper.selectByExample(collectExample);

        return collectList != null && collectList.size() == 1 ? 1 : 0;
    }

    @Override
    public PageData selectAll(Integer userId, Byte type, Integer page, Integer limit) {
        MarketCollectExample example = new MarketCollectExample();
        example.createCriteria().andUserIdEqualTo(userId)
                .andTypeEqualTo(type)
                .andDeletedEqualTo(false);
        example.setOrderByClause("add_Time desc");
        PageHelper.startPage(page, limit);
        List<MarketCollect> collects = collectMapper.selectByExample(example);
        //封装分页信息
        PageData pageData = PageData.list(collects);

        //--->使用流进行封装
        List<CollectListData> collectList = collects.stream().map(collect -> {
            Integer goodsId = collect.getValueId();
            MarketGoods marketGoods = goodsMapper.selectByPrimaryKey(goodsId);
            return CollectListData.builder()
                    .brief(marketGoods.getBrief())
                    .id(collect.getId())
                    .name(marketGoods.getName())
                    .picUrl(marketGoods.getPicUrl())
                    .retailPrice(marketGoods.getRetailPrice())
                    .type(type)
                    .valueId(goodsId)
                    .build();
        }).collect(Collectors.toList());

        // 设置处理后的列表数据到PageData对象中
        pageData.setList(collectList);
        return pageData;
    }

    @Override
    public Integer addOrDeleteCollect(CollectAddOrDeleteVo collectAddOrDeleteVo) {
        Byte type = collectAddOrDeleteVo.getType();
        Integer valueId = collectAddOrDeleteVo.getValueId();

        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Integer userId = (Integer) subject.getPrincipal();
            int affectedRows = 0;
            //在收藏夹里--->delete=1
            if (hasCollect(userId, valueId, type) == 1) {
                //这里使用 直接删除
                MarketCollectExample example = new MarketCollectExample();
                example.createCriteria().andUserIdEqualTo(userId)
                        .andTypeEqualTo(type)
                        .andValueIdEqualTo(valueId)
                        .andDeletedEqualTo(false);
                affectedRows = collectMapper.deleteByExample(example);
            } else {
                //不在收藏夹里--->insert
                Date current = new Date();
                MarketCollect collect = MarketCollect.builder()
                        .userId(userId)
                        .valueId(valueId)
                        .type(type)
                        .addTime(current)
                        .updateTime(current)
                        .deleted(false)
                        .build();
                affectedRows = collectMapper.insertSelective(collect);
            }
            return affectedRows;
        }
        return -1;//未登录
    }
}
